package com.ruoyi.enterprise.service.impl;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.enterprise.domain.EnterpriseArchive;
import com.ruoyi.enterprise.mapper.EnterpriseArchiveMapper;
import com.ruoyi.enterprise.service.IEnterpriseArchiveService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-10
 */
@Service
public class EnterpriseArchiveServiceImpl implements IEnterpriseArchiveService
{
    private static final Logger log = LoggerFactory.getLogger(EnterpriseArchiveServiceImpl.class);
    /**
     * 111
     */
    @Autowired
    private EnterpriseArchiveMapper enterpriseArchiveMapper;

    /**
     * 查询企业基本信息
     * 
     * @param epId 企业基本信息ID
     * @return 企业基本信息
     */
    @Override
    public EnterpriseArchive selectEnterpriseArchiveById(Long epId)
    {
        return enterpriseArchiveMapper.selectEnterpriseArchiveById(epId);
    }

    /**
     * 查询企业基本信息列表
     * 
     * @param enterpriseArchive 企业基本信息
     * @return 企业基本信息
     */
    @Override
    public List<EnterpriseArchive> selectEnterpriseArchiveList(EnterpriseArchive enterpriseArchive)
    {
        return enterpriseArchiveMapper.selectEnterpriseArchiveList(enterpriseArchive);
    }

    /**
     * 新增企业基本信息
     * 
     * @param enterpriseArchive 企业基本信息
     * @return 结果
     */
    @Override
    public int insertEnterpriseArchive(EnterpriseArchive enterpriseArchive)
    {
        enterpriseArchive.setCreateTime(DateUtils.getNowDate());
        return enterpriseArchiveMapper.insertEnterpriseArchive(enterpriseArchive);
    }

    /**
     * 修改企业基本信息
     * 
     * @param enterpriseArchive 企业基本信息
     * @return 结果
     */
    @Override
    public int updateEnterpriseArchive(EnterpriseArchive enterpriseArchive)
    {
        enterpriseArchive.setUpdateTime(DateUtils.getNowDate());
        return enterpriseArchiveMapper.updateEnterpriseArchive(enterpriseArchive);
    }

    /**
     * 批量删除企业基本信息
     * 
     * @param epIds 需要删除的企业基本信息ID
     * @return 结果
     */
    @Override
    public int deleteEnterpriseArchiveByIds(Long[] epIds)
    {
        return enterpriseArchiveMapper.deleteEnterpriseArchiveByIds(epIds);
    }

    /**
     * 删除企业基本信息信息
     * 
     * @param epId 企业基本信息ID
     * @return 结果
     */
    @Override
    public int deleteEnterpriseArchiveById(Long epId)
    {
        return enterpriseArchiveMapper.deleteEnterpriseArchiveById(epId);
    }

    @Override
    public EnterpriseArchive selectEnterpriseArchiveByUserName(String epUserName) {
        return enterpriseArchiveMapper.selectEnterpriseArchiveByUserName(epUserName);
    }

    /**
     * 导入用户数据
     *
     * @param epList        用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importEnterPrise(List<EnterpriseArchive> epList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(epList) || epList.size() == 0) {
            throw new CustomException("导入企业数据不能为空" + "！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (EnterpriseArchive ep : epList) {
            try {
//                // 验证是否存在这个用户
//                EnterpriseArchive xx = new EnterpriseArchive();
//                //xx.setEpName(ep.getEpName());
//                xx.setEpUserName(operName);
                System.out.println("当前登录的企业用户名是：-------------------"+operName);
                EnterpriseArchive u = enterpriseArchiveMapper.selectEnterpriseArchiveByUserName(operName);
                if (u==null) {
                    ep.setCreateBy(operName);
                    this.insertEnterpriseArchive(ep);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、企业 " + ep.getEpName() + " 导入成功");
                } else if (u!=null) {
                    ep.setEpId(u.getEpId());
                    ep.setUpdateBy(operName);
                    this.updateEnterpriseArchive(ep);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、企业 " + ep.getEpName() + " 更新成功");
                } else if (isUpdateSupport) {
                    ep.setUpdateBy(operName);
                    this.updateEnterpriseArchive(ep);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、企业 " + ep.getEpName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、企业 " + ep.getEpName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + ep.getEpName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
